<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<title>${name_china}管理列表</title>
<%@include file="/common/head.jsp" %>

<!-- 时间日期插件 -->
<link href="<%=request.getContextPath()%>/static/plugin/My97DatePicker/skin/default/datepicker.css" rel="stylesheet" >
<script src="<%=request.getContextPath()%>/static/plugin/My97DatePicker/WdatePicker.js"></script>
<style>
.maskback{
	position:fixed;
	width:100%;
	height:100%;
	top:0px;
	background:rgba(0,0,0,0.7);
}
.mainback{
    width: 40%;
    height: 200px;
    background: #fff;
    margin: 0 auto;
    margin-top: 17%;
}
</style>
</head>
<body class="iframe-page">
<div class="page-title-wrapper">
    <div class="page-title">
        <div class="page-title-text">
            <span>${name_china}管理</span>
        </div>
        <div id="toolbar" style="float: right"></div>
    </div>
    <div class="page-title-border"></div>
</div>

<div class="search-field">
    <span class="field-cont">
        <span class="field-cont-title">订单号码</span>
        <input type="text" class="summer-input" id="orderNum"/>
    </span>
    
     <span class="field-cont">
        <span class="field-cont-title">推广位名称</span>
        <input type="text" class="summer-input" id="tgwName"/>
    </span>
    
    <span class="field-cont">
        <span class="field-cont-title">推广位ID</span>
        <input type="text" class="summer-input" id="tgwId"/>
    </span>
    
    <span class="field-cont">
        <span class="field-cont-title">是否结算</span>
        <input type="text" class="summer-input" id="isJiesuan"/>
    </span>
    
     <span class="field-cont">
        <span class="field-cont-title">下单开始时间</span>
        <input type="text" class="summer-input" id="startTime" onFocus="WdatePicker({dateFmt:'yyyy-MM-dd'})"/>
    </span>
    
    <span class="field-cont">
        <span class="field-cont-title">下单结束时间</span>
        <input type="text" class="summer-input" id="endTime" onFocus="WdatePicker({dateFmt:'yyyy-MM-dd',minDate:'#F{$dp.$D(\'startTime\')}'})"/>
    </span> 
    
    
    <span class="field-btn-wrapper">
        <a id="search" class="btn btn-primary btn-icon wave distance-right"><i class="icon icon-search"></i></a>
        <a id="reset" class="btn btn-icon wave wave-dark"><i class="icon icon-refresh"></i></a>
    </span>
</div>

<div id="t-wrap">
    <table class="summer-datagrid" id="dataList">
	    <thead>
		    <tr>
		        <th>订单号码</th>
		        <th>下单时间</th>
		        <th>订单总金额</th>
				<th>京东返佣金</th>
				<th>店主返佣金</th>
				<th>推广位ID</th>
				<th>推广位名称</th>
				<th>云店主名称</th>
				<th>是否结算</th>
		        <th>操作</th>
		    </tr>
	    </thead>
    </table>
</div>

<div style="display: none" id="order" class="maskback">
     <div class="mainback">
     	 <div class="form-cont" style="top:40px;">
	        <p class="form-cont-title"><span class="field-required">*</span>选择日期</p>
	        <input type="text" id="oneDay" name="oneDay" class="summer-input summer-validatebox"  data-validatebox="validType:['required']" onFocus="WdatePicker({dateFmt:'yyyyMMdd'})"/> 
	     </div>
	     <div class="form-btn-wrapper" id="funcbar" style="margin-top: 100px;">
	        <a id="save" href="javascript:;" class="btn btn-ok btn-normal  wave distance-right" data-id="b8efebfe7a21464fb0793800758ebb2a" data-url="/system/category/add" data-mark="create" data-permit="category:add">保存</a>
	        <a class="btn btn-normal" id="backBtn">关闭</a>
	     </div>
     </div>
</div>

<div style="display: none" id="orderOver" class="maskback">
     <div class="mainback">
     	 <div class="form-cont" style="top:40px;">
	        <p class="form-cont-title"><span class="field-required">*</span>选择日期</p>
	        <input type="text" id="oneDayOver" name="oneDayOver" class="summer-input summer-validatebox"  data-validatebox="validType:['required']" onFocus="WdatePicker({dateFmt:'yyyyMMdd'})"/> 
	     </div>
	     <div class="form-btn-wrapper" id="funcbar" style="margin-top: 100px;">
	        <a id="saveOver" href="javascript:;" class="btn btn-ok btn-normal  wave distance-right" data-id="b8efebfe7a21464fb0793800758ebb2a" data-url="/system/category/add" data-mark="create" data-permit="category:add">保存</a>
	        <a class="btn btn-normal" id="backBtnOver">关闭</a>
	     </div>
     </div>
</div>

<script>
	//请求url
	var CONST_URL = {
		LIST: basePath + '${common_url}list',
	   	INIT_ADD: basePath + "${common_url}initAdd",
	   	INIT_EDIT: basePath + "${common_url}initEdit",
	   	INIT_VIEW: basePath + "${common_url}initView",
	   	DEL: basePath + "${common_url}delete",
	   	EXP: basePath + "${common_url}exp",
	   	SYNC_CURRENT: basePath + "${common_url}syncCurrent",
		SYNC_ONE: basePath + "${common_url}syncOne",
		SYNC_ALL: basePath + "${common_url}syncAll",
		SYNC_CURRENT_OVER: basePath + "${common_url}syncCurrentOver",
		SYNC_ONE_OVER: basePath + "${common_url}syncOneOver",
		SYNC_ALL_OVER: basePath + "${common_url}syncAllOver",
	   	INIT_IMP: basePath + "${common_url}initImp",
	   	NOWSYNC: basePath + "${common_url}nowsync",
	   	EXP_ORDER: basePath + "${common_url}expOrder",
	};
	
    var  permission = CommonUtil.getPermission('${name_english}');
    
    $(function () {
    	
    	//关闭按钮
    	$("#backBtn").click(function(){
    		$("#order").hide();
    	})
    	$("#save").on("click",function(){
    		var oneDay = $("#oneDay").val();
    		submitOrder(oneDay);
    	});
    	
    	//关闭按钮
    	$("#backBtnOver").click(function(){
    		$("#orderOver").hide();
    	})
    	$("#saveOver").on("click",function(){
    		var oneDayOver = $("#oneDayOver").val();
    		//alert(oneDayOver)
    		submitOrderOver(oneDayOver);
    	});
        // 新建toobar
		$.funcbar({
			id: 'toolbar',
			type: 'toolbar',
			data: permission,
			permit: ['${name_english}:exp','${name_english}:syncCurrent','${name_english}:syncOne','${name_english}:syncAll','${name_english}:syncCurrentOver','${name_english}:syncOneOver','${name_english}:syncAllOver'],
		});
        
        // 查询按钮
        $('#search').on('click', reloadTable);
        
        //重置按钮
        $('#reset').on('click', function () {
        	//重置文本框的值
        	$('#orderNum').val('');
        	$('#tgwName').val('');
        	$('#tgwId').val('');
        	$('#isJiesuan').val('');
        	$('#startTime').val(''); 
        	$('#endTime').val('');
        	
        	reloadTable();
        });
      //新建
        $('#toolbar').on('click', 'a[data-permit="${name_english}:syncOne"]', function() {
        	//window.location.href = CONST_URL.INIT_ADD; 
        	$("#order").show();
        	
		});
      
        //导出订单
        $('#toolbar').on('click', 'a[data-permit="${name_english}:expOrder"]', function() {
        	expOrder();
		});
        //新建
        $('#toolbar').on('click', 'a[data-permit="${name_english}:syncCurrent"]', function() {
        	//window.location.href = CONST_URL.INIT_ADD; 
        	$.sdialog({
				type: 'confirm',
				msg: '您确定要同步今天的京东订单记录吗?',
				onConfirm: function(result) {
				    if (result) {
				    	$.sajax({
			                url : CONST_URL.SYNC_CURRENT,
			                onLoadSuccess : function() {
			                	 $.stip({
                	                type:'ok',//显示类型 有info、 ok、 danger
                	                text: '删除成功！'    
			                	});
			                	reloadTable();  
			                }
			            });
				    		
				    }	
				}
			});
		});
        
        //新建
        $('#toolbar').on('click', 'a[data-permit="${name_english}:syncAll"]', function() {
        	//window.location.href = CONST_URL.INIT_ADD; 
        	$.sdialog({
				type: 'confirm',
				msg: '您确定要同步今天的京东订单记录吗?',
				onConfirm: function(result) {
				    if (result) {
				    	$.sajax({
			                url : CONST_URL.SYNC_ALL,
			                onLoadSuccess : function(data) {
			                	 $.stip({
                	                type:'ok',//显示类型 有info、 ok、 danger
                	                text: data.msg    
			                	 });
			                	 reloadTable();  
			                }
			            });
				    		
				    }	
				}
			});
		});
        
        
        //新建
        $('#toolbar').on('click', 'a[data-permit="${name_english}:syncOneOver"]', function() {
        	//window.location.href = CONST_URL.INIT_ADD; 
        	$("#orderOver").show();
        	
		});
       
        //新建
        $('#toolbar').on('click', 'a[data-permit="${name_english}:syncCurrentOver"]', function() {
        	//window.location.href = CONST_URL.INIT_ADD; 
        	$.sdialog({
				type: 'confirm',
				msg: '您确定要结算今天的京东订单记录吗?',
				onConfirm: function(result) {
				    if (result) {
				    	$.sajax({
			                url : CONST_URL.SYNC_CURRENT_OVER,
			                onLoadSuccess : function() {
			                	 $.stip({
                	                type:'ok',//显示类型 有info、 ok、 danger
                	                text: '删除成功！'    
			                	});
			                	reloadTable();  
			                }
			            });
				    		
				    }	
				}
			});
		});
        
        //新建
        $('#toolbar').on('click', 'a[data-permit="${name_english}:syncAllOver"]', function() {
        	//window.location.href = CONST_URL.INIT_ADD; 
        	$.sdialog({
				type: 'confirm',
				msg: '您确定要结算今天的京东订单记录吗?',
				onConfirm: function(result) {
				    if (result) {
				    	$.sajax({
			                url : CONST_URL.SYNC_ALL_OVER,
			                onLoadSuccess : function() {
			                	 $.stip({
                	                type:'ok',//显示类型 有info、 ok、 danger
                	                text: '删除成功！'    
			                	});
			                	reloadTable();  
			                }
			            });
				    		
				    }	
				}
			});
		});
        
        
        
        
        
       createTable();
        
        //查看
        $('#t-wrap').on('click', 'a[data-permit="${name_english}:syncCurrent"]', function() {
        	var $this = $(this),
        	    id = $this.closest('tr').find('td[data-field="id"]').attr('data-value');
        	window.location.href = CONST_URL.INIT_VIEW +"?id=" + id;
        });
       
        //编辑
        $('#t-wrap').on('click', 'a[data-permit="${name_english}:initEdit"]', function() {
            var $this = $(this),
                id = $this.closest('tr').find('td[data-field="id"]').attr('data-value');
            window.location.href = CONST_URL.INIT_EDIT + '?id=' + id;
        });	
        
        //删除
		$('#t-wrap').on('click', 'a[data-permit="${name_english}:delete"]', function() {
			var $this = $(this),
			    id = $this.closest('tr').find('td[data-field="id"]').attr('data-value');
			$.sdialog({
				type: 'confirm',
				msg: '您确定要删除选中的记录吗?',
				onConfirm: function(result) {
				    if (result) {
				    	$.sajax({
			                url : CONST_URL.DEL + "?ids=" + id,
			                onLoadSuccess : function() {
			                	 $.stip({
                	                type:'ok',//显示类型 有info、 ok、 danger
                	                text: '删除成功！'    
			                	});
			                	reloadTable();  
			                }
			            });
				    		
				    }	
				}
			});
		});
        
		//删除
		$('#t-wrap').on('click', 'a[data-permit="${name_english}:nowsync"]', function() {
			var $this = $(this),
			    id = $this.closest('tr').find('td[data-field="id"]').attr('data-value');
			$.sdialog({
				type: 'confirm',
				msg: '您确定要同步选中的记录吗?',
				onConfirm: function(result) {
				    if (result) {
				    	$.sajax({
			                url : CONST_URL.NOWSYNC + "?id=" + id,
			                onLoadSuccess : function() {
			                	 $.stip({
                	                type:'ok',//显示类型 有info、 ok、 danger
                	                text: '同步成功！'    
			                	});
			                	reloadTable(); 
			                }
			            });
				    		
				    }	
				}
			});
		});
        
        //导出 
        $('#toolbar').on('click', 'a[data-permit="${name_english}:exp"]', function() {
        	expOrder(); 	  	
		});
        
        //导入 
        $('#toolbar').on('click', 'a[data-permit="${name_english}:initImp"]', function() {
        	window.location.href = CONST_URL.INIT_IMP;	
		});
        
    });
    
    // 重新加载表格
	function reloadTable() {
		$('#dataList').datagrid('reload',{
            ajax: {
            	param: {
            		"orderNum": trimSpace($('#orderNum').val()),
            		"tgwName":trimSpace($('#tgwName').val()),
            		"tgwId":trimSpace($('#tgwId').val()),
            		"isJiesuan":trimSpace($('#isJiesuan').val()),
                	"startTime": trimSpace($('#startTime').val()),
                	"endTime": trimSpace($('#endTime').val())
            	}
            }
        });
	}
    
    //创建表格
    function createTable() {
    	$('#dataList').datagrid({
    		ajax: {
    			url: CONST_URL.LIST,
    			param: {
    				"orderNum": trimSpace($('#orderNum').val()),
            		"tgwName":trimSpace($('#tgwName').val()),
            		"tgwId":trimSpace($('#tgwId').val()),
            		"isJiesuan":trimSpace($('#isJiesuan').val()),
                	"startTime": trimSpace($('#startTime').val()),
                	"endTime": trimSpace($('#endTime').val())
            	}
    		},
    		columns: [
    		     {
    		    	 field: 'id',
    		    	 hidden: true
    		     },
    		     {
    		    	 field: 'orderNum',
    		    	 render: function (rowData) {
    		    		 return '<a href="javascript:;" data-permit="${name_english}:initView" class="link">'+ rowData.orderNum +'</a>';
    		    	 }
    		     },
    		     {
    		    	 field: 'orderDate'
    		     },
    		     {
    		    	 field: 'orderTotalMoney'
    		     },
    		     {
    		    	 field: 'commisions'
    		     },
    		     {
    		    	 field: 'marketProfit'
    		     },
    		     {
    		    	 field: 'tgwId'
    		     },
    		     {
    		    	 field: 'tgwName'
    		     },
    		     {
    		    	 field: 'yunUserNickName'
    		     },
    		     {
    		    	 field: 'isJiesuan'
    		     },
    		    {
    		    	 render: function (rowData) {
    		    		 if( "未匹配到用户推广位"==rowData.isJiesuan ){
    		    			 return $.funcbar({
        		    			 data: permission,    //请求的数据
        		    			 type: 'text',     //类型
        		    			 display: 2,    //显示数量
        		    			 permit: ['${name_english}:nowsync'] //需要的权限按钮
        		    		 }); 
    		    		 }
    		    	 }
    		    }
    		          
    	    ]
    	});
    }
  //导出订单方法
    function expOrder() {
        //得到查询条件中的value和创建表单
        
        var tgwId = trimSpace($('#tgwId').val()),
    	    startTime = trimSpace($('#startTime').val()),
    	    endTime = trimSpace($('#endTime').val()),
       		tgwName = trimSpace($('#tgwName').val());
        var form = document.createElement('form');//创建表单
        form.method = 'post';//表单提交方式
        form.action = CONST_URL.EXP_ORDER;//路径
        document.body.appendChild(form);

        //创建查询条件(input隐藏)
        //全称
        if ( tgwName ) {
            var inputName = document.createElement('input');
            inputName.type = 'hidden';
            inputName.value = tgwName;
            inputName.name = 'tgwName';
            form.appendChild(inputName );
        } 
        if ( tgwId ) {
            var inputName = document.createElement('input');
            inputName.type = 'hidden';
            inputName.value = tgwId;
            inputName.name = 'tgwId';
            form.appendChild(inputName );
        } 
        if ( startTime ) {
            var inputName = document.createElement('input');
            inputName.type = 'hidden';
            inputName.value = startTime;
            inputName.name = 'startTime';
            form.appendChild(inputName );
        } 
        if ( endTime ) {
            var inputName = document.createElement('input');
            inputName.type = 'hidden';
            inputName.value = endTime;
            inputName.name = 'endTime';
            form.appendChild(inputName );
        } 
        		       
        //如果没有任何条件就提示用户
        if ( tgwName || tgwId || startTime || endTime) {
            form.submit();
        } else {
            $.sdialog({
                 type : 'confirm',
                 msg : '未添加导出条件，默认为全部导出，确定吗？',
                 onConfirm : function(result) {
                     if (result) {
                         form.submit();
                     }
                 }
             });
        }
    }
    //导出方法
    function exp() {
        //得到查询条件中的value和创建表单
        
        var name = trimSpace($('#name').val()),
		    category = $('#category').combobox('getValue');
    	    //startTime = trimSpace($('#startTime').val()),
    	    //endTime = trimSpace($('#endTime').val());
        
        var form = document.createElement('form');//创建表单
        form.method = 'post';//表单提交方式
        form.action = CONST_URL.EXP;//路径
        document.body.appendChild(form);

        //创建查询条件(input隐藏)
        //全称
        if ( name ) {
            var inputName = document.createElement('input');
            inputName.type = 'hidden';
            inputName.value = name;
            inputName.name = 'name';
            form.appendChild(inputName );
        }
        
        if ( category ) {
            var inputCategory = document.createElement('input');
            inputCategory.type = 'hidden';
            inputCategory.value = category;
            inputCategory.name = 'category';
            form.appendChild(inputCategory);
        }
        		       
        //如果没有任何条件就提示用户
        if ( name || category  ) {
            form.submit();
        } else {
            $.sdialog({
                 type : 'confirm',
                 msg : '未添加导出条件，默认为全部导出，确定吗？',
                 onConfirm : function(result) {
                     if (result) {
                         form.submit();
                     }
                 }
             });
        }
    }
    
    //表单提交
    function submitOrder(oneDay) {
        $.ajax({
     	   url:CONST_URL.SYNC_ONE,
     	   data:{"date":oneDay},
     	   success:function(data){
     		   $.stip({
  	                type:'ok',//显示类型 有info、 ok、 danger
  	                text: '保存成功！',
  	                onBeforeHide: function () {
  	                	$("#order").hide();
  	               }
              	});
     	   }
        });
    }
    
    //表单提交
    function submitOrderOver(oneDay) {
        $.ajax({
     	   url:CONST_URL.SYNC_ONE_OVER,
     	   data:{"date":oneDay},
     	   success:function(data){
     		   $.stip({
  	                type:'ok',//显示类型 有info、 ok、 danger
  	                text: '保存成功！',
  	                onBeforeHide: function () {
  	                	$("#orderOver").hide();
  	               }
              	});
     	   }
        });
    }
    
   
</script>
</body>
</html>